{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "563dcbff",
   "metadata": {},
   "source": [
    "### 数据结构练习\n",
    "\n",
    "```\n",
    "购物车程序项目：\n",
    "\n",
    "要求：1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "\n",
    "　　   2、让用户输入商品编号进行商品的购买。\n",
    "\n",
    "　　   3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "\n",
    "　　   4、用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "a6039b36",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-25T03:12:04.373709Z",
     "start_time": "2021-10-25T03:11:53.403180Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的支付余额：8888\n",
      "------ goods details -------\n",
      "商品编号: 0, 商品名称：python课本，商品的价格：60 元\n",
      "------ goods details -------\n",
      "商品编号: 1, 商品名称：华为手机，商品的价格：8000 元\n",
      "------ goods details -------\n",
      "商品编号: 2, 商品名称：小米手环，商品的价格：199 元\n",
      "------ goods details -------\n",
      "商品编号: 3, 商品名称：喜茶，商品的价格：30 元\n",
      "请正确输入购买商品的编号：3\n",
      "[喜茶]已加入您的购物车，您的账号余额还有： 8858 元\n",
      "如若继续购买，请输入正确的编号：停止购买，请输入 q   谢谢！\n",
      "------ goods details -------\n",
      "商品编号: 0, 商品名称：python课本，商品的价格：60 元\n",
      "------ goods details -------\n",
      "商品编号: 1, 商品名称：华为手机，商品的价格：8000 元\n",
      "------ goods details -------\n",
      "商品编号: 2, 商品名称：小米手环，商品的价格：199 元\n",
      "------ goods details -------\n",
      "商品编号: 3, 商品名称：喜茶，商品的价格：30 元\n",
      "请正确输入购买商品的编号：\n",
      "请输入商品编号！！！\n",
      "------ goods details -------\n",
      "商品编号: 0, 商品名称：python课本，商品的价格：60 元\n",
      "------ goods details -------\n",
      "商品编号: 1, 商品名称：华为手机，商品的价格：8000 元\n",
      "------ goods details -------\n",
      "商品编号: 2, 商品名称：小米手环，商品的价格：199 元\n",
      "------ goods details -------\n",
      "商品编号: 3, 商品名称：喜茶，商品的价格：30 元\n",
      "请正确输入购买商品的编号：q\n",
      "用户购物车： [['喜茶', 30]]\n",
      "您的余额还有:  8858\n"
     ]
    }
   ],
   "source": [
    "# 商品的数据信息\n",
    "# goods_list = [[],[],...[]]  #列表的嵌套 \n",
    "goods_list =[ \n",
    "    #[商品名称，商品价格]\n",
    "    [\"python课本\",60],\n",
    "    [\"华为手机\",8000],\n",
    "    [\"小米手环\",199],\n",
    "    [\"喜茶\",30]\n",
    "]\n",
    "\n",
    "# 也可以用元组！\n",
    "goods_list =[ \n",
    "    #[商品名称，商品价格]\n",
    "    (\"python课本\",60),\n",
    "    (\"华为手机\",8000),\n",
    "    \n",
    "]\n",
    "\n",
    "# 用户购物车信息\n",
    "user_shopping_car = []\n",
    "\n",
    "# -----知识点：打印与用户输入------\n",
    "# 用户输入（余额），使用 input ，  input 的使用，结果是一个字符串             \n",
    "Alipay = int(input(\"请输入您的支付余额：\"))\n",
    "\n",
    "while True:\n",
    "    # 循环遍历商品列表，将商品信息按照一定的格式输出\n",
    "    # 一定的格式： 商品编号：XX , 商品名陈，XX , 商品价格： XX\n",
    "\n",
    "    for i in range(len(goods_list)):     # range 会根据商品列表的长度变化而变化\n",
    "        print(\"------ goods details -------\")\n",
    "        print(\"商品编号: %s, 商品名称：%s，商品的价格：%s 元\"%(i,goods_list[i][0],goods_list[i][1]))\n",
    "       \n",
    "\n",
    "    # 2.让用户输入商品编号进行商品的购买\n",
    "    # 2.1  用户输入的内容是否符合规范（用户买的东西有没有）？\n",
    "\n",
    "    # 思考：用户除了输入数值外，有没有可能输入不是数值\n",
    "    choice = input(\"请正确输入购买商品的编号：\")\n",
    "    if choice.isdigit():   # isdigit 判断字符串里面的内容是否为数值\n",
    "        choice = int(choice)\n",
    "        if choice > len(goods_list):\n",
    "        # b. 如果没有商品，返回没有\n",
    "            print(\"您选择的商品不存在\")\n",
    "        else:\n",
    "            # a. 如果有，判断金额是否足够： 用户金额 Alipay 商品的金额 goods_list[choice][1]\n",
    "            if Alipay > goods_list[choice][1]:\n",
    "                # 用户金额够  1.增加该商品到用户的购物车  2.扣除金额\n",
    "                user_shopping_car.append(goods_list[choice])\n",
    "                Alipay = Alipay - goods_list[choice][1]\n",
    "                print(\"[%s]已加入您的购物车，您的账号余额还有： %s 元\"%(goods_list[choice][0],Alipay))\n",
    "                print(\"如若继续购买，请输入正确的编号：停止购买，请输入 q   谢谢！\")\n",
    "            else:\n",
    "                print(\"您的余额不足，请更换支付方式\")\n",
    "    elif choice == \"q\":\n",
    "        # 用户退出购买\n",
    "        print(\"用户购物车：\", user_shopping_car)\n",
    "        print(\"您的余额还有: \", Alipay)\n",
    "        break\n",
    "    else:\n",
    "        print(\"请输入商品编号！！！\")\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fcb8bdec",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
